package com.keiou.drink.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;

import com.keiou.drink.R;
import com.keiou.drink.api.AbsSubscriber;
import com.keiou.drink.api.exception.ApiException;
import com.keiou.drink.api.remote.Api;
import com.keiou.drink.api.rx.RxHelper;
import com.keiou.drink.response.OrderStatusRes;
import com.keiou.drink.response.base.HttpResult;
import com.keiou.drink.ui.activity.base.RxActivity;
import com.keiou.drink.util.CToast;
import com.keiou.drink.util.TDevice;
import com.keiou.drink.util.UIUtil;

import butterknife.BindView;

/**
 * 作者 ：Bob on 2017/10/30.
 * 邮箱 ：motherlovefather@126.com
 */

public class ResultActivity extends RxActivity {
    @BindView(R.id.iv_status)
    ImageView ivStatus;

    private String orderId;

    private Handler handler = new Handler();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_result;
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        ivStatus.setImageResource(R.drawable.making);
    }

    @Override
    protected void initData() {
        orderId = getIntent().getExtras().getString("orderId");
    }

    @Override
    protected void loadData() {
        delayLoadData(1000);
    }

    private void delayLoadData(long delay) {
        if (isFinishing()) {
            return;
        }

        handler.postDelayed(() -> loadStatus(), delay);
    }

    private void loadStatus() {

        if (isFinishing()) {
            return;
        }


        addSubscribe(Api.getInstance().getOrderStatus(TDevice.getDeviceID(this), orderId)
                .compose(RxHelper.applySchedulers())
                .subscribe(new AbsSubscriber<HttpResult<OrderStatusRes>>() {
                    @Override
                    public void onNext(HttpResult<OrderStatusRes> result) {
                        OrderStatusRes orderMsgRes = result.getResult();
                        String status = orderMsgRes.getStatus();
                        if ("1".equals(status)) {
                            delayLoadData(3000);
                        } else if ("2".equals(status)) {
                            takeFood();
                        } else {
                            CToast.showShort(ResultActivity.this, "订单状态出错");
                        }
                    }


                    @Override
                    protected void onError(ApiException ex) {
                        delayLoadData(3000);
                    }

                    @Override
                    protected void onIntercept(int code) {
                        onInterceptCode(code);
                    }
                }));
    }

    @Override
    protected void onDestroy() {
        handler.removeCallbacksAndMessages(null);
        super.onDestroy();
    }


    private void takeFood() {
        ivStatus.setImageResource(R.drawable.take_food);

        handler.postDelayed(() -> UIUtil.redirectTo(ResultActivity.this, MainActivity.class), 5000);

    }


}
